package com.ld.admin.controller.usercenter;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ld.admin.controller.AdminBasicController;
import com.ld.admin.model.system.SysUserTempLogModel;
import com.ld.admin.model.system.SysUserTempModel;
import com.ld.admin.service.user.SysUserTempService;
import com.ld.shieldsb.annotation.duplicate.SubmitRequestIntercept;
import com.ld.shieldsb.common.core.model.Result;
import com.ld.shieldsb.common.core.util.StringUtils;
import com.ld.shieldsb.common.web.util.Web;
import com.ld.shieldsb.dao.model.QueryModel;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * 临时账号
 * 
 * @ClassName SysUserTempController
 * @author <a href="mailto:donggongai@126.com" target="_blank">吕凯</a>
 * @date 2019年09月30日 17:06:01
 *
 */
@Api(tags = "临时账号")
@Controller(value = "usercenterSysUserTempController")
@RequestMapping("${shieldsb.adminPath}/usercenter/sysusertemp")
public class SysUserTempController extends AdminBasicController<SysUserTempModel> {
    @Autowired
    protected SysUserTempService sysUserTempService;

    @ApiOperation(value = "首页", notes = "")
    @RequestMapping(path = "", method = RequestMethod.GET)
    public String index() {
        return "forward:" + adminPath + "/usercenter/sysusertemp/toSearch"; // 跳转搜索页面
    }

    @ApiOperation(value = "到搜索页面", notes = "")
    @RequestMapping(path = "/toSearch", method = RequestMethod.GET)
    public String toSearch() {
        return "a/usercenter/sysusertemp/sysusertemp_search";
    }

    @ApiOperation(value = "搜索结果", notes = "")
    @RequestMapping(path = "/search", method = RequestMethod.POST)
    public String search() {
        QueryModel queryModel = sysUserTempService.getSearchQueryModel(request, getModelClass());
        sysUserTempService.getPageBean(request, getModelClass(), queryModel);
        return "a/usercenter/sysusertemp/sysusertemp_list";
    }

    @ApiOperation(value = "临时账号展示", notes = "")
    @RequestMapping(path = "/show", method = RequestMethod.GET)
    public String show() {
        long id = getLong("id");
        sysUserTempService.getOne(request, getModelClass(), id);
        return "a/usercenter/sysusertemp/sysusertemp_show";
    }

    @ApiOperation(value = "跳转到添加/修改临时账号", notes = "")
    @RequestMapping(path = "/toAddOrUpdate", method = RequestMethod.GET)
    public String toAddOrUpd() {
        long id = getLong("id");
        if (id == 0) {
            request.setAttribute("randomPwd", StringUtils.getRandomStr(16));
        }
        sysUserTempService.getOne(request, getModelClass(), id);
        return "a/usercenter/sysusertemp/sysusertemp_update";
    }

    @ApiOperation(value = "添加/修改临时账号", notes = "")
    @RequestMapping(path = "/addOrUpdate", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    @SubmitRequestIntercept(desp = "提交拦截验证")
    public Object addOrUpd(@Valid SysUserTempModel model) {
        if (model.getPassword().equals("******")) {
            model.setPassword(null); // 更新
        }
        Result result = sysUserTempService.addOrUpdate(model, model.getId() != null);
        return Web.Response.result(result);
    }

    @ApiOperation(value = "停用临时账号", notes = "")
    @RequestMapping(path = "/disable", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    @SubmitRequestIntercept(desp = "提交拦截验证")
    public Object disable() throws ServletException, IOException {
        long id = getLong("id");
        Result result = sysUserTempService.disable(getModelClass(), id);
        return Web.Response.result(result);
    }

    @ApiOperation(value = "启用临时账号", notes = "")
    @RequestMapping(path = "/enable", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    @SubmitRequestIntercept(desp = "提交拦截验证")
    public Object enable() throws ServletException, IOException {
        long id = getLong("id");
        Result result = sysUserTempService.enable(getModelClass(), id);
        return Web.Response.result(result);
    }

//    @ApiOperation(value = "删除临时账号", notes = "")
//    @RequestMapping(path = "/del", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object del() {
        long id = getLong("id");
        Result result = sysUserTempService.remove(getModelClass(), id);
        return Web.Response.result(result);
    }

    @ApiOperation(value = "到临时账号日志搜索页面", notes = "")
    @RequestMapping(path = "/toSearchLog", method = RequestMethod.GET)
    public String toSearchLog() {
        setAttributes();
        return "a/usercenter/sysusertemp/sysusertemplog_search";
    }

    @ApiOperation(value = "临时账号日志搜索结果", notes = "")
    @RequestMapping(path = "/searchLog", method = RequestMethod.POST)
    public String searchLog() {
        long id = getLong("id");
        QueryModel queryModel = new QueryModel();
        queryModel = Web.Parameter.dealReqParams(request, SysUserTempLogModel.class, queryModel);
        queryModel.addEq(SysUserTempLogModel::getAccountId, id);
        queryModel.setOrder(SysUserTempLogModel::getCreateTime, true);
        sysUserTempService.getPageBean(request, SysUserTempLogModel.class, queryModel);
        return "a/usercenter/sysusertemp/sysusertemplog_list";
    }

}